%macro FUNCTION 1
section .text
global _%1
_%1:
 .begin:
%endmacro


FUNCTION Memory_Set
	cld
	mov		edi, [esp +  4]
	mov		eax, [esp +  8]
	mov		ecx, [esp + 12]

 .set8:
	shr		ecx, 1
	jnc		.set16
	stosb
 .set16:
	shr		ecx, 1
	jnc		.set32
	stosw
 .set32:
  	rep
  	stosd

 .done:
	ret


FUNCTION Memory_Copy
	cld
	mov		edi, [esp +  4]
	mov		esi, [esp +  8]
	mov		ecx, [esp + 12]

 .copy8:
	shr		ecx, 1
	jnc		.copy16
	movsb
 .copy16:
	shr		ecx, 1
	jnc		.copy32
	movsw
 .copy32:
  	rep
  	movsd

 .done:
 	ret


FUNCTION Memory_Compare
    cld
	mov		edi, [esp +  4]
	mov		esi, [esp +  8]
	mov		ecx, [esp + 12]

 .compare8:
	shr		ecx, 1
	jnc		.compare16
	cmpsb
	jne     .not_equal8
 .compare16:
	shr		ecx, 1
	jnc		.compare32
	cmpsw
	jne     .not_equal16
 .compare32:
  	rep
  	cmpsd
	jne     .not_equal32

 .not_equal8:
    xor     eax, eax
    mov     al, byte [edi - 1]
    mov     ecx, dword [esi - 1]
    sub     eax, ecx
    jmp     .done
 .not_equal16:
    xor     eax, eax
    mov     ax, word [edi - 1]
    mov     ecx, dword [esi - 1]
    sub     eax, ecx
    jmp     .done
 .not_equal32:
    xor     eax, eax
    mov     eax, dword [edi - 1]
    mov     ecx, dword [esi - 1]
    sub     eax, ecx
    jmp     .done

 .done:
 	ret
